package com.lxg.dao;

import com.lxg.pojo.History;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

@Mapper
public interface HistoryDao {

    /**
     * 查询全部借阅的历史记录
     * @return
     */
    @Select("select * from history where is_deleted = 0")
    public ArrayList<History> getAllHistory();

    /**
     * 查询全部未还借阅记录
     * @return
     */
    @Select("select * from history where status=0")
    public ArrayList<History> getAllBorrowing();

    /**
     * 查询全部已还借阅记录
     * @return
     */
    @Select("select * from history where status=1")
    public ArrayList<History> getAllReturned();

    /**
     * 根据uid查询该用于所有借阅记录
     * @param uid
     * @return
     */
    @Select("select * from history where uid=#{uid} and is_deleted = 0")
    public ArrayList<History> getAllHistoryByUid(Integer uid);

    /**
     * 根据uid查询全部未还借阅记录
     * @return
     */
    @Select("select * from history where status=0 and uid=#{uid}")
    public ArrayList<History> getAllBorrowingByUid(Integer uid);

    /**
     * 根据uid查询全部已还借阅记录
     * @return
     */
    @Select("select * from history where status=1 and uid=#{uid}")
    public ArrayList<History> getAllReturnedByUid(Integer uid);

    /**
     * 根据hid查询历史记录
     * @return
     */
    @Select("select * from history where hid=#{hid}")
    public History getHistoryByHid(Integer hid);

    /**
     *  根据uid和bid查询某用户是否已经借了某本书
     * @param uid
     * @param bid
     * @return
     */
    @Select("select * from history where uid=#{uid} and bid=#{bid} and is_deleted = 0")
    public History getHistoryByUidAndBid(Integer uid,Integer bid);


    @Select("select status from history where uid=#{uid} and bid=#{bid} and is_deleted = 0")
    public Integer getHistoryStatus(Integer uid,Integer bid);


    /**
     * 添加一条记录
     * @param uid
     * @param bid
     * @param bookname
     * @param author
     * @param begintime
     * @param endtime
     */
    @Insert("insert into history (uid,bid,bookname,author,begintime,endtime,num,status) values " +
            "(#{uid},#{bid},#{bookname},#{author},#{begintime},#{endtime},#{num},#{status})")
    public int addHistory(Integer uid,Integer bid,String bookname,String author,
                           String begintime,String endtime,Integer num,Integer status);


    /**
     * 修改一条记录
     * @param uid
     * @param bid
     * @param endtime
     */
    @Update("update history set endtime=#{endtime},num=#{num} where uid=#{uid} and bid=#{bid}")
    public void updateHistory(Integer uid,Integer bid,String endtime,Integer num);





    //删除记录
    @Update("update history set is_deleted=1 where uid=#{uid} and bid=#{bid}")
    boolean deleteHistory(Integer uid, Integer bid);


    @Update("update history set status=1 where uid=#{uid} and bid=#{bid}")
    boolean updateHistoryStatus(Integer uid, Integer bid);


    @Select("select * from history where  is_deleted = 0 and status=0")
    ArrayList<History> getAllHistoryByStatus();


    @Select("select * from history where  is_deleted = 0 and bookname like CONCAT('%',#{bookname},'%') ")
    ArrayList<History> getAllHistoryByLikeBookName(String bookname);
}
